package com.juzi.wopu.web;
import org.apache.commons.lang3.StringUtils;

import com.juzi.wopu.domain.Employee;
import com.juzi.wopu.service.IEmployeeService;
import com.opensymphony.xwork2.ActionContext;

public class ChangePasswordAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	IEmployeeService employeeService;
	//老密码
	private String oldPassword;
	//新密码
	private String password;
	//重复密码
	private String repassword;
	//修改用户密码
	@Override
	public String execute() throws Exception {
		Employee user = (Employee)ActionContext.getContext().getSession().get(USER_IN_SESSION);
		employeeService.changePassword(user.getId(),password);
		return SUCCESS;
	}
	
	@Override
	public void validate() {
		//从session中拿到当前登录用户
		Employee user = (Employee)ActionContext.getContext().getSession().get(USER_IN_SESSION);
		//判断用户的密码是否输入正确
		if(oldPassword!=null && !oldPassword.equals(user.getPassword())){
			this.addFieldError("errorMsg", "旧密码错误！！");
			return;
		}
		//判断新密码是否为空
		if(StringUtils.isBlank(password)){
			this.addFieldError("errorMsg", "新密码不能为空！！");
			return;
		}
		//判断重复密码是否一致
		if(!password.equals(repassword)){
			this.addFieldError("errorMsg", "两次密码不一致！！");
			return;
		}
	}
	
	
	//用来跳转的
	@Override
	public String input() throws Exception {
		return INPUT;
	}

	public String getOldPassword() {
		return oldPassword;
	}

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRepassword() {
		return repassword;
	}

	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	public IEmployeeService getEmployeeService() {
		return employeeService;
	}

	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}
}